home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 11 / Mac Magazin and MacEasy Magazine CD - Issue 11.iso / Sharewarebibliothek / Entwickler / WASTE 1.1b1 Distribution / Demo Source / WEDemoFiles.p < prev    next >
Text File  |  1995-06-01  |  7KB  |  299 lines

  1. unit WEDemoFiles;
  2.  
  3. { WASTE DEMO PROJECT: }
  4. { File Handling }
  5.  
  6. { Copyright © 1993-1995 Marco Piovanelli }
  7. { All Rights Reserved }
  8.  
  9. interface
  10.     uses
  11.         WEDemoIntf;
  12.  
  13.     function ReadTextFile (pFileSpec: FSSpecPtr;
  14.                                     hWE: WEHandle): OSErr;
  15.     function WriteTextFile (pFileSpec: FSSpecPtr;
  16.                                     hWE: WEHandle): OSErr;
  17.     function TranslateDrag (theDrag: DragReference;
  18.                                     theItem: ItemReference;
  19.                                     requestedType: FlavorType;
  20.                                     dataHandle: Handle): OSErr;
  21.  
  22. implementation
  23.  
  24.     function ReadTextFile (pFileSpec: FSSpecPtr;
  25.                                     hWE: WEHandle): OSErr;
  26.         var
  27.             dataForkRefNum, resForkRefNum: Integer;
  28.             hText, hStyles, hSoup: Handle;
  29.             textSize: Size;
  30.  
  31.         procedure CleanUp;
  32.         begin
  33.             ForgetHandle(hText);
  34.             ForgetHandle(hStyles);
  35.             ForgetHandle(hSoup);
  36.  
  37.             if (dataForkRefNum > 0) then
  38.                 begin
  39.                     if (FSClose(dataForkRefNum) <> noErr) then
  40.                         ;
  41.                     dataForkRefNum := 0;
  42.                 end;
  43.  
  44.             if (resForkRefNum > 0) then
  45.                 begin
  46.                     CloseResFile(resForkRefNum);
  47.                     resForkRefNum := 0;
  48.                 end;
  49.         end;  { CleanUp }
  50.  
  51.         procedure CheckErr (err: OSErr);
  52.         begin
  53.             if (err <> noErr) then
  54.                 begin
  55.                     ReadTextFile := err;
  56.                     CleanUp;
  57.                     Exit(ReadTextFile);
  58.                 end;
  59.         end;  { CheckErr }
  60.  
  61.     begin
  62.         ReadTextFile := noErr;
  63.         dataForkRefNum := 0;
  64.         resForkRefNum := 0;
  65.         hText := nil;
  66.         hStyles := nil;
  67.         hSoup := nil;
  68.  
  69. { open the data fork with read-only permission }
  70.         CheckErr(FSpOpenDF(pFileSpec^, fsRdPerm, dataForkRefNum));
  71.  
  72. { get data fork size }
  73.         CheckErr(GetEOF(dataForkRefNum, textSize));
  74.  
  75. { try to allocate a handle that large; use temporary memory if available }
  76.         CheckErr(NewHandleTemp(textSize, hText));
  77.  
  78. { read in the text }
  79.         CheckErr(FSRead(dataForkRefNum, textSize, hText^));
  80.  
  81. { see if the file has a resource fork }
  82.         resForkRefNum := FSpOpenResFile(pFileSpec^, fsRdPerm);
  83.         if (resForkRefNum > 0) then
  84.             begin
  85.  
  86. { look for a style scrap resource (get the first one; the resource ID doesn't matter) }
  87.                 hStyles := Get1IndResource(kTypeStyles, 1);
  88.  
  89. { look for a soup resource as well }
  90.                 hSoup := Get1IndResource(kTypeSoup, 1);
  91.             end;
  92.  
  93. { insert the text into the WE record }
  94.         HLock(hText);
  95.         CheckErr(WEInsert(hText^, textSize, StScrpHandle(hStyles), hSoup, hWE));
  96.  
  97. { set the insertion point at the beginning of the text }
  98.         WESetSelection(0, 0, hWE);
  99.  
  100. { reset the WE instance modification count }
  101.         WEResetModCount(hWE);
  102.  
  103. { clean up and exit }
  104.         CleanUp;
  105.  
  106.     end;  { ReadTextFile }
  107.  
  108.     function WriteTextFile (pFileSpec: FSSpecPtr;
  109.                                     hWE: WEHandle): OSErr;
  110.         var
  111.             dataForkRefNum, resForkRefNum: Integer;
  112.             hText, hStyles, hSoup: Handle;
  113.             fileInfo: FInfo;
  114.             textSize: Size;
  115.             replacing: Boolean;
  116.             err: OSErr;
  117.  
  118.         procedure CleanUp;
  119.         begin
  120.             ForgetResource(hStyles);
  121.             ForgetResource(hSoup);
  122.  
  123.             if (dataForkRefNum > 0) then
  124.                 begin
  125.                     if (FSClose(dataForkRefNum) <> noErr) then
  126.                         ;
  127.                     dataForkRefNum := 0;
  128.                 end;
  129.  
  130.             if (resForkRefNum > 0) then
  131.                 begin
  132.                     CloseResFile(resForkRefNum);
  133.                     resForkRefNum := 0;
  134.                 end;
  135.  
  136.         end;  { CleanUp }
  137.  
  138.         procedure CheckErr (err: OSErr);
  139.         begin
  140.             if (err <> noErr) then
  141.                 begin
  142.                     WriteTextFile := err;
  143.                     ErrorAlert(err);
  144.                     CleanUp;
  145.                     Exit(WriteTextFile);
  146.                 end;
  147.         end;  { CheckErr }
  148.  
  149.     begin
  150.         WriteTextFile := noErr;
  151.         dataForkRefNum := 0;
  152.         resForkRefNum := 0;
  153.         hText := nil;
  154.         hStyles := nil;
  155.         hSoup := nil;
  156.  
  157. { are we replacing an existing file? }
  158.         err := FSpGetFInfo(pFileSpec^, fileInfo);
  159.         if (err = noErr) then
  160.             replacing := true
  161.         else if (err = fnfErr) then
  162.             replacing := false
  163.         else
  164.             CheckErr(err);
  165.  
  166. { delete existing file, if any }
  167.         if (replacing) then
  168.             CheckErr(FSpDelete(pFileSpec^));
  169.  
  170. { create a new file }
  171.         FSpCreateResFile(pFileSpec^, kAppSignature, kTypeText, 0);
  172.         CheckErr(ResError);
  173.  
  174. { if replacing an old file, copy the old file information }
  175.         if (replacing) then
  176.             CheckErr(FSpSetFInfo(pFileSpec^, fileInfo));
  177.  
  178. { open the data fork for writing }
  179.         CheckErr(FSpOpenDF(pFileSpec^, fsRdWrPerm, dataForkRefNum));
  180.  
  181. { get the text handle from the WE instance }
  182. { WEGetText returns the original handle, not a copy, so don't dispose of it! }
  183.         hText := WEGetText(hWE);
  184.         textSize := GetHandleSize(hText);
  185.  
  186. { write the text }
  187.         CheckErr(FSWrite(dataForkRefNum, textSize, hText^));
  188.  
  189. { open the resource file for writing }
  190.         resForkRefNum := FSpOpenResFile(pFileSpec^, fsRdWrPerm);
  191.         CheckErr(ResError);
  192.  
  193. { allocate temporary handles to hold the style scrap and the soup }
  194.         CheckErr(NewHandleTemp(0, hStyles));
  195.         CheckErr(NewHandleTemp(0, hSoup));
  196.  
  197. { create the style scrap and the soup }
  198.         CheckErr(WECopyRange(0, maxLongInt, nil, StScrpHandle(hStyles), hSoup, hWE));
  199.  
  200. { make them resource handles }
  201.         AddResource(hStyles, kTypeStyles, 128, '');
  202.         CheckErr(ResError);
  203.         AddResource(hSoup, kTypeSoup, 128, '');
  204.         CheckErr(ResError);
  205.  
  206. { write them to the resource file }
  207.         WriteResource(hStyles);
  208.         CheckErr(ResError);
  209.         WriteResource(hSoup);
  210.         CheckErr(ResError);
  211.  
  212. { "clean" this document by resetting the WE instance modification count }
  213.         WEResetModCount(hWE);
  214.  
  215. { clean up }
  216.         CleanUp;
  217.  
  218.     end;  { WriteTextFile }
  219.  
  220.     function TranslateDrag (theDrag: DragReference;
  221.                                     theItem: ItemReference;
  222.                                     requestedType: FlavorType;
  223.                                     dataHandle: Handle): OSErr;
  224.  
  225. { this simple routine is meant to give an idea of how the drag translation hook ('xdrg') }
  226. { is supposed to work -- in the real world I should probably handle styled text files, }
  227. { PICT files and maybe other fancier file types here: }
  228. { that is left as an exercise for the reader }
  229.  
  230.         var
  231.             numFlavors: Integer;
  232.             theType: FlavorType;
  233.             hfs: HFSFlavor;
  234.             refNum: Integer;
  235.             dataSize: Size;
  236.  
  237.         procedure CleanUp;
  238.         begin
  239.             if (refNum <> 0) then
  240.                 begin
  241.                     if (FSClose(refNum) <> noErr) then
  242.                         ;
  243.                     refNum := 0;
  244.                 end;
  245.         end;  { CleanUp }
  246.  
  247.         procedure CheckErr (err: OSErr);
  248.         begin
  249.             if (err <> noErr) then
  250.                 begin
  251.                     TranslateDrag := err;
  252.                     CleanUp;
  253.                     Exit(TranslateDrag);
  254.                 end;
  255.         end;  { CheckErr }
  256.  
  257.     begin
  258.         TranslateDrag := badDragFlavorErr;        { assume failure }
  259.         refNum := 0;
  260.  
  261. { we'll try to translate HFS objects to TEXT, so make sure that is the requested type }
  262.         if (requestedType <> kTypeText) then
  263.             Exit(TranslateDrag);
  264.  
  265. { see if this drag item is a TEXT file }
  266.         dataSize := SizeOf(hfs);
  267.         if (CountDragItemFlavors(theDrag, theItem, numFlavors) = noErr) then
  268.             if (numFlavors = 1) then
  269.                 if (GetFlavorType(theDrag, theItem, 1, theType) = noErr) then
  270.                     if (theType = flavorTypeHFS) then
  271.                         if (GetFlavorData(theDrag, theItem, theType, @hfs, dataSize, 0) = noErr) then
  272.                             if (hfs.fileType = kTypeText) then
  273.                                 begin
  274.                                     TranslateDrag := noErr;        { assume success }
  275.  
  276. { if dataHandle is NIL, we're finished }
  277.                                     if (dataHandle = nil) then
  278.                                         Exit(TranslateDrag);
  279.  
  280. { open the file for reading }
  281.                                     CheckErr(FSpOpenDF(hfs.fileSpec, fsRdPerm, refNum));
  282.  
  283. { get file size }
  284.                                     CheckErr(GetEOF(refNum, dataSize));
  285.  
  286. { resize the data handle }
  287.                                     SetHandleSize(dataHandle, dataSize);
  288.                                     CheckErr(MemError);
  289.  
  290. { read the file }
  291.                                     CheckErr(FSRead(refNum, dataSize, dataHandle^));
  292.                                 end;
  293.  
  294. { clean up }
  295.         CleanUp;
  296.  
  297.     end;  { TranslateDrag }
  298.  
  299. end.